/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <aknviewappui.h>
#include <eikmenub.h>
#include <avkon.hrh>
#include <akncontext.h>
#include <akntitle.h>
#include <stringloader.h>
#include <barsread.h>
#include <LDIR.rsg>
#include "LDIR.hrh"
#include "LDIRWasteView.h"
#include "LDIRWaste.hrh"
#include "LDIRWaste.h"
#include "Settings.h"

/**
 * First phase of Symbian two-phase construction. Should not contain any
 * code that could leave.
 */
CLDIRWasteView::CLDIRWasteView()
	{
	}

/** 
 * The view's destructor removes the container from the control
 * stack and destroys it.
 */
CLDIRWasteView::~CLDIRWasteView()
	{
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance then calls the second-phase constructor
 * without leaving the instance on the cleanup stack.
 * @return new instance of CLDIRWasteView
 */
CLDIRWasteView* CLDIRWasteView::NewL()
	{
	CLDIRWasteView* self = CLDIRWasteView::NewLC();
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Symbian two-phase constructor.
 * This creates an instance, pushes it on the cleanup stack,
 * then calls the second-phase constructor.
 * @return new instance of CLDIRWasteView
 */
CLDIRWasteView* CLDIRWasteView::NewLC()
	{
	CLDIRWasteView* self = new ( ELeave ) CLDIRWasteView();
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
	}

/**
 * Second-phase constructor for view.  
 * Initialize contents from resource.
 */
void CLDIRWasteView::ConstructL()
	{
	BaseConstructL( R_LDIRWASTE_LDIRWASTE_VIEW );
	}

/**
 * @return The UID for this view
 */
TUid CLDIRWasteView::Id() const
	{
	return TUid::Uid( ELDIRWasteViewId );
	}

/**
 * Handle a command for this view (override)
 * @param aCommand command id to be handled
 */
void CLDIRWasteView::HandleCommandL( TInt aCommand )
	{
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{ // code to dispatch to the AknView's menu and CBA commands is generated here
		default:
			break;
		}

	if ( !commandHandled )
		{

		}
	}

/**
 *    Handles user actions during activation of the view, 
 *    such as initializing the content.
 */
void CLDIRWasteView::DoActivateL( const TVwsViewId& /*aPrevViewId*/,
	TUid /*aCustomMessageId*/, const TDesC8& /*aCustomMessage*/)
	{
	SetupStatusPaneL();

	if ( iLDIRWaste == NULL )
		{
		CSettings& settings = CSettings::InstanceL();
		__ASSERT_ALWAYS(settings.WorkingWaste(), User::Invariant());
		CLDIRWaste* form = CLDIRWaste::NewL( *settings.WorkingWaste(), this );
		form->SetMopParent( this );
		form->ExecuteLD( R_LDIRWASTE_LDIRWASTE );
		AppUi()->AddToStackL( *this, form );
		iLDIRWaste = form;
		}
	}

/**
 */
void CLDIRWasteView::DoDeactivate()
	{
	CleanupStatusPane();

	if ( iLDIRWaste != NULL )
		{
		AppUi()->RemoveFromStack( iLDIRWaste );
		delete iLDIRWaste;
		iLDIRWaste = NULL;
		}
	}

/** 
 * Handle status pane size change for this view (override)
 */
void CLDIRWasteView::HandleStatusPaneSizeChange()
	{
	CAknView::HandleStatusPaneSizeChange();

	// this may fail, but we're not able to propagate exceptions here
	TVwsViewId view;
	AppUi()->GetActiveViewId( view );
	if ( view.iViewUid == Id() )
		{
		TInt result;
		TRAP( result, SetupStatusPaneL() );
		}
	}

// [[[ begin generated function: do not modify
void CLDIRWasteView::SetupStatusPaneL()
	{
	// reset the context pane
	TUid contextPaneUid = TUid::Uid( EEikStatusPaneUidContext );
	CEikStatusPaneBase::TPaneCapabilities subPaneContext =
		StatusPane()->PaneCapabilities( contextPaneUid );
	if ( subPaneContext.IsPresent() && subPaneContext.IsAppOwned() )
		{
		CAknContextPane* context =
			static_cast<CAknContextPane*> ( StatusPane()->ControlL(
				contextPaneUid ) );
		context->SetPictureToDefaultL();
		}

	// setup the title pane
	TUid titlePaneUid = TUid::Uid( EEikStatusPaneUidTitle );
	CEikStatusPaneBase::TPaneCapabilities subPaneTitle =
		StatusPane()->PaneCapabilities( titlePaneUid );
	if ( subPaneTitle.IsPresent() && subPaneTitle.IsAppOwned() )
		{
		CAknTitlePane
			* title = static_cast<CAknTitlePane*> ( StatusPane()->ControlL(
				titlePaneUid ) );
		TResourceReader reader;
		iEikonEnv->CreateResourceReaderLC( reader, R_LDIRWASTE_TITLE_RESOURCE );
		title->SetFromResourceL( reader );
		CleanupStack::PopAndDestroy(); // reader internal state
		}

	}

void CLDIRWasteView::CleanupStatusPane()
	{
	}

